package com.twohand.admin.Controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.twohand.admin.Annotation.Permission;
import com.twohand.admin.Dto.BaseQueryDto;
import com.twohand.admin.Entity.Menu;
import com.twohand.admin.Entity.Role;
import com.twohand.admin.Entity.User;
import com.twohand.admin.Service.CosService;
import com.twohand.admin.Service.MenuService;
import com.twohand.admin.Service.RoleService;
import com.twohand.admin.Service.UserService;
import com.twohand.admin.Util.JwtUtil;
import com.twohand.admin.Util.PageResponse;
import com.twohand.admin.Vo.HomeInfo;
import com.twohand.admin.Vo.MenuPageListVo;
import com.twohand.admin.Vo.MenuParentOptions;
import com.twohand.utils.Response.BaseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    MenuService menuService;
    @Autowired
    RoleService roleService;

    @Autowired
    CosService cosService;

    @Autowired
    UserService userService;
    @GetMapping
    HomeInfo getMenu(){
        User currentUser = JwtUtil.getCurrentUser();
        Integer id = currentUser.getId();

        if(id == null){
            throw new BaseException(500,"服务器保存id错误");
        }
        List<Menu> menuList = menuService.getUserMenuList(id);
        List<Role> roleList = roleService.getRoleList();
        List<String> permission = menuService.getPermissionByUserId(id);
        User user = userService.getUserById(id);

//        List<Cos> cosList = cosService.getBucketList();
        HomeInfo info = new HomeInfo();
        info.setMenu(menuList);
        info.setRole(roleList);
        info.setPermission(permission);
        info.setBucketList(new ArrayList<>());
        info.setUser(user);
        return info;
    }

    @GetMapping("/base")
    PageResponse<Menu> getBaseMenu(BaseQueryDto baseQueryDto){
        List<Menu> menuList = menuService.getBaseMenu();
        List<MenuParentOptions> parentOptions = menuService.getParent();
        PageHelper.startPage(baseQueryDto.getPageNum(),baseQueryDto.getPageSize());
        PageInfo<Menu> pageInfo = new PageInfo<>(menuList);
        return new MenuPageListVo(pageInfo,menuList,menuService.getMenuType(),parentOptions);
    }

    @Permission(value = "menu:add")
    @PostMapping
    Boolean insertMenu(@RequestBody Menu menu){
        return menuService.addMenu(menu);
    }

    @Permission(value = "menu:edit")
    @PutMapping
    Boolean updateMenu(@RequestBody Menu menu){
        return menuService.updateMenu(menu);
    }

    @Permission(value = "menu:delete")
    @DeleteMapping("/{id}")
    Boolean delMenu(@PathVariable Integer id){
        return menuService.delMenu(id);
    }
}
